/*
 * =====================================================================================
 *
 *       Filename:  test_udp.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2019年05月21日 15时25分20秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <libnyb/libnyb-net.h>
int fd[200];
char *buff = "1234567890"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm"
"1234567890qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm";


int send_udp(int fd, int i)
{
    int len = strlen(buff);
    while(1) {
        if(send(fd, buff, len, 0) < 0) {
            fprintf(stderr, "send 10.10.10.112:%d, %s\n", 30000 + 4*i, strerror(errno));
        }
    }
}

int main(int argc, char **argv)
{
    int i = 0;
    for(i = 0; i < 200; i++) {
        fd[i] = udpsock_create("10.10.10.106", 15060 + 2*i);
        if(fd[i] < 0) {
            continue;
        }
        if(udpsock_connect(fd[i], "10.10.10.112", 30000 + 4*i) < 0) {
            continue;
        }
    }

    int len = strlen(buff);
    for(i = 0; i < 200; i++) {
        if(fd[i] > 0) {
            switch(fork()) {
            case 0:
                send_udp(fd[i], i);
                exit(0);
            case -1:
                perror("fork()");
                break;
            default:
                break;
            }
        }
    }
    while(1) sleep(10);

    return 0;
}

